/*
 * @Description: 订单校验规则DS
 * @Version: 1.0
 * @Autor: c-jack.qian
 * @Date: 2020-12-28 17:05:24
 * @LastEditors: c-jack.qian
 * @LastEditTime: 2021-03-30 16:35:59
 */
import { DataSet } from 'choerodon-ui/pro/lib';
import { DataSetSelection, FieldType } from 'choerodon-ui/pro/lib/data-set/enum';
import commonConfigStore from '@common/config/commonConfig';

import intl from 'utils/intl';

const O2_OM = process.env.NODE_ENV === 'development' ? '/o2om-c' : commonConfigStore.O2_OM;

// let controlModelist = [];
// 上级
const orderTypeOptions1 = new DataSet({
  selection: 'single' as DataSetSelection,
  autoQuery: true,
  paging: false,
  transport: {
    read: config => {
      return {
        ...config,
        url: `/hitf/v1/dji-external-interface/get-order-type`,
        data: [],
        method: 'post',
        transformResponse: res => {
          return JSON.parse(res).data;
        },
      };
    },
  },
});

// 订单类型
const orderTypeOptions2 = new DataSet({
  selection: 'single' as DataSetSelection,
  autoQuery: true,
  paging: false,
  transport: {
    read: config => {
      return {
        ...config,
        url: `/hitf/v1/dji-external-interface/get-order-type`,
        data: [],
        method: 'post',
        transformResponse: res => {
          // return JSON.parse(res).data;
          const result = JSON.parse(res).data;
          let data: any = [];
          if (result && result instanceof Array) {
            result.forEach(item => {
              data = data.concat(item.sonTypes);
            });
          }
          return data;
        },
      };
    },
  },
});

const orderCheckRuleListDS = () => ({
  autoQuery: true,
  selection: 'multiple',
  primaryKey: 'id',
  transport: {
    read: config => {
      return {
        ...config,
        url: `${O2_OM}/v1/order-check-rules`,
        method: 'get',
      };
    },
    create: config => {
      return {
        ...config,
        data: config.data[0],
        url: `${O2_OM}/v1/order-check-rules`,
        method: 'post',
      };
    },
    update: config => {
      console.log(config.data[0]);
      const { isInvalid } = config.data[0];
      const url = isInvalid
        ? `${O2_OM}/v1/order-check-rules/invalid`
        : `${O2_OM}/v1/order-check-rules`;
      return {
        ...config,
        data: config.data[0],
        url,
        method: 'put',
      };
    },
    destroy: config => {
      return {
        ...config,
        url: `${O2_OM}/v1/order-check-rules`,
        method: 'delete',
      };
    },
  },
  fields: [
    // {
    //     name: 'orderTypeName',
    //     type: FieldType.string,
    //     label: intl.get('dji.otc.om.view.field.orderType').d('订单类型'),
    //     required: true,
    // },
    {
      name: 'orderTypeCodeObj',
      type: FieldType.object,
      label: intl.get('dji.otc.om.view.field.orderTypeName').d('订单类型'),
      options: orderTypeOptions2,
      required: true,
      textField: 'name',
      valueField: 'id',
      ignore: 'always',
    },
    {
      name: 'orderTypeCode',
      type: FieldType.number,
      label: intl.get('dji.otc.om.view.field.orderTypeName').d('订单类型'),
      required: true,
      bind: 'orderTypeCodeObj.id',
    },
    {
      name: 'orderTypeName',
      type: FieldType.string,
      label: intl.get('dji.otc.om.view.field.orderTypeName').d('订单类型'),
      required: true,
      bind: 'orderTypeCodeObj.name',
    },
    {
      name: 'processNodeCode',
      type: FieldType.string,
      label: intl.get('dji.otc.om.view.field.processNodeCode').d('处理节点'),
      lookupCode: 'DJIOM.PROCESS_NODE',
      required: true,
    },
    {
      name: 'beanId',
      type: FieldType.string,
      label: intl.get('dji.otc.om.view.field.BeanId').d('BeanId'),
      required: true,
    },
    {
      name: 'checkRuleCode',
      type: FieldType.string,
      label: intl.get('dji.otc.om.view.field.checkRuleCode').d('校验规则'),
      required: true,
      lookupCode: 'DJIOM.CHECK_RULE',
    },
    {
      name: 'priority',
      type: FieldType.number,
      label: intl.get('dji.otc.om.view.field.priority').d('顺序'),
      required: true,
    },
    {
      name: 'controlModeCode',
      type: FieldType.string,
      label: intl.get('dji.otc.om.view.field.controlModeCode').d('控制方式'),
      lookupCode: 'DJIOM.CONTROL_MODE',
      required: true,
    },
    {
      name: 'checkRuleStatusCode',
      type: FieldType.string,
      label: intl.get('dji.otc.om.view.field.checkRuleStatusCode').d('状态'),
      lookupCode: 'DJIOM.CHECK_RULE_STATUS',
    },
    {
      name: 'createdUserName',
      type: FieldType.string,
      label: intl.get('dji.otc.om.view.field.createdUserName').d('创建人'),
    },
    {
      name: 'creationDate',
      type: FieldType.dateTime,
      label: intl.get('dji.otc.om.view.field.creationDate').d('创建时间'),
    },
  ],
  queryFields: [
    {
      name: 'orderTypeCode',
      type: FieldType.number,
      label: intl.get('dji.otc.om.view.field.orderTypeName').d('订单类型'),
      options: orderTypeOptions2,
      textField: 'name',
      valueField: 'id',
    },
    {
      name: 'parentOrderTypeCode',
      type: FieldType.number,
      label: intl.get('dji.otc.om.view.field.parentOrderTypeCode').d('上级'),
      options: orderTypeOptions1,
      textField: 'name',
      valueField: 'id',
    },
    {
      name: 'checkRuleStatusCode',
      type: FieldType.string,
      label:  intl.get('dji.otc.om.view.field.checkRuleStatusCode').d('状态'),
      lookupCode: 'DJIOM.CHECK_RULE_STATUS',
    },
    {
      name: 'processNodeCode',
      type: FieldType.string,
      label:  intl.get('dji.otc.om.view.field.processNodeCode').d('处理节点'),
      lookupCode: 'DJIOM.PROCESS_NODE',
    },
    {
      name: 'checkRuleCode',
      type: FieldType.string,
      label:  intl.get('dji.otc.om.view.field.checkRuleCode').d('校验规则'),
      lookupCode: 'DJIOM.CHECK_RULE',
    },
  ],
});

const createDS = () => ({
  autoQuery: true,
  selection: false,
  primaryKey: 'id',
  paging: false,
  transport: {
    create: config => {
      return {
        ...config,
        data: config.data[0],
        url: `${O2_OM}/v1/order-check-rules`,
        method: 'post',
      };
    },
  },
  fields: [
    {
      name: 'orderTypeCodeObj',
      type: FieldType.object,
      label: intl.get('dji.otc.om.view.field.orderType').d('订单类型'),
      options: orderTypeOptions2,
      required: true,
      textField: 'name',
      valueField: 'id',
      ignore: 'always',
    },
    {
      name: 'orderTypeCode',
      type: FieldType.number,
      label: intl.get('dji.otc.om.view.field.orderTypeName').d('订单类型'),
      required: true,
      bind: 'orderTypeCodeObj.id',
    },
    {
      name: 'orderTypeName',
      type: FieldType.string,
      label: intl.get('dji.otc.om.view.field.orderTypeName').d('订单类型'),
      required: true,
      bind: 'orderTypeCodeObj.name',
    },
    {
      name: 'processNodeCode',
      type: FieldType.string,
      label: intl.get('dji.otc.om.view.field.processNodeCode').d('处理节点'),
      lookupCode: 'DJIOM.PROCESS_NODE',
      required: true,
    },
    {
      name: 'beanId',
      type: FieldType.string,
      label: intl.get('dji.otc.om.view.field.BeanId').d('BeanId'),
      required: true,
      lookupCode: 'DJIOM.RULE_BEAN_ID',
      textField: 'meaning',
      valueField: 'value',
    },
    // {
    //   name: 'beanId',
    //   type: FieldType.string,
    //   label: intl.get('dji.otc.om.view.field.tagName').d('BeanId'),
    //   required: true,
    //   lookupCode: 'DJIOM.RULE_BEAN_ID'
    // },
    {
      name: 'checkRuleCode',
      type: FieldType.string,
      label: intl.get('dji.otc.om.view.field.checkRuleCode').d('校验规则'),
      required: true,
      lookupCode: 'DJIOM.CHECK_RULE',
    },
    {
      name: 'priority',
      type: FieldType.number,
      label: intl.get('dji.otc.om.view.field.priority').d('顺序'),
      required: true,
    },
    {
      name: 'controlModeCode',
      type: FieldType.object,
      label: intl.get('dji.otc.om.view.field.controlModeCode').d('控制方式'),
      lookupCode: 'DJIOM.CONTROL_MODE',
      required: true,
    },
    {
      name: 'checkRuleStatusCode',
      type: FieldType.string,
      label: intl.get('dji.otc.om.view.field.checkRuleStatusCode').d('状态'),
      lookupCode: 'DJIOM.CHECK_RULE_STATUS',
      required: true,
      defaultValue: 'valid',
    },
    {
      name: 'createdUserName',
      type: FieldType.string,
      label: intl.get('dji.otc.om.view.field.createdUserName').d('创建人'),
    },
    {
      name: 'creationDate',
      type: FieldType.dateTime,
      label: intl.get('dji.otc.om.view.field.creationDate').d('创建时间'),
    },
  ],
});

export { orderCheckRuleListDS, createDS };
